package com.hongdee.crm.tdc.mapper;

import com.hongdee.crm.tdc.pojo.MarketNairePoJo;
import com.hongdee.crm.tdc.pojo.MarketNaireScorePoJo;
import com.hongdee.crm.tdc.pojo.MarketQuestionLibraryPojo;
import com.hongdee.crm.tdc.pojo.MarketQuestionnairePojo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author zhangkai
 * @desc 一句话描述
 **/
@Mapper
public interface MarketQuestionMapper {
    /************ 问题库 *****************/
    /***
     * 根据父ID查询子记录
     * @param pId
     * @param brandCode 默认为NUll 或空
     * @return
     * @throws Exception
     */
    @SelectProvider(type = MarkertQuestionSQL.class, method = "queryLibrarySQL")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "label", property = "label"),
            @Result(column = "brand_code", property = "brandCode"),
            @Result(column = "label_type", property = "labelType"),
            @Result(column = "value_type", property = "valueType"),
            @Result(column = "p_id", property = "pId"),
            @Result(column = "dis_order", property = "disOrder"),
            @Result(column = "status", property = "status"),
    })
    List<MarketQuestionLibraryPojo> queryLibrary(@Param("pId") String pId, @Param("brandCode") String brandCode,
                                                 @Param("isTrue") boolean isTrue, @Param("status") String status) throws Exception;


    /**
     * 新增问题记录或问题库
     *
     * @param po
     * @throws Exception
     */

    @InsertProvider(type = MarkertQuestionSQL.class, method = "addLibrarySQL")
    void addLibrary(MarketQuestionLibraryPojo po) throws Exception;

    /**
     * 编辑问题记录或问题库
     *
     * @param po
     * @throws Exception
     */

    @UpdateProvider(type = MarkertQuestionSQL.class, method = "updateLibrarySQL")
    void updateLibrary(MarketQuestionLibraryPojo po) throws Exception;


    /**
     * 修改问题项状态
     *
     * @param id
     * @param status
     * @throws Exception
     */
    @UpdateProvider(type = MarkertQuestionSQL.class, method = "updateLibraryStatusSQL")
    void updateLibraryStatus(@Param("id") String id, @Param("status") String status) throws Exception;

    /**
     * 查询是否已存在对应的记录
     *
     * @param label
     * @param id
     * @throws Exception
     */
    @SelectProvider(type = MarkertQuestionSQL.class, method = "existLibrarySQL")
    List<MarketQuestionLibraryPojo> existsLibraryStatus(@Param("label") String label, @Param("pId") String pId, @Param("id") String id) throws Exception;


    /***
     * 查询标签项详情
     * @param id
     * @return
     * @throws Exception
     */
    @SelectProvider(type = MarkertQuestionSQL.class, method = "queryLibraryInfoSQL")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "label", property = "label"),
            @Result(column = "brand_code", property = "brandCode"),
            @Result(column = "label_type", property = "labelType"),
            @Result(column = "value_type", property = "valueType"),
            @Result(column = "p_id", property = "pId"),
            @Result(column = "dis_order", property = "disOrder"),
            @Result(column = "status", property = "status"),
    })
    MarketQuestionLibraryPojo queryLibraryInfoSQL(@Param("id") String id) throws Exception;


    /******
     *
     * 调研问卷基础信息
     * ******/

    @SelectProvider(type = MarkertQuestionSQL.class, method = "queryNaireSQL")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "code", property = "code"),
            @Result(column = "name", property = "name"),
            @Result(column = "type", property = "type"),
            @Result(column = "total_score", property = "totalScore"),
            @Result(column = "company_id", property = "companyId"),
            @Result(column = "status", property = "status"),
    })
    List<MarketNairePoJo> queryNaire(@Param("companyId") String companyId, @Param("code") String code);


    @SelectProvider(type = MarkertQuestionSQL.class, method = "queryNaireInfoByCodeSQL")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "code", property = "code"),
            @Result(column = "name", property = "name"),
            @Result(column = "type", property = "type"),
            @Result(column = "total_score", property = "totalScore"),
            @Result(column = "company_id", property = "companyId"),
            @Result(column = "status", property = "status"),
    })
    MarketNairePoJo queryNaireInfoByCode(@Param("companyId") String companyId, @Param("code") String code);

    /***
     * 查询问卷信息详情
     * @param id
     * @return
     */
    @SelectProvider(type = MarkertQuestionSQL.class, method = "queryNaireInfoSQL")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "code", property = "code"),
            @Result(column = "name", property = "name"),
            @Result(column = "type", property = "type"),
            @Result(column = "total_score", property = "totalScore"),
            @Result(column = "company_id", property = "companyId"),
            @Result(column = "status", property = "status"),
    })
    MarketNairePoJo queryNaireInfo(@Param("id") String id);


    /**
     * 修改问卷状态
     *
     * @param id
     * @param status
     * @throws Exception
     */
    @UpdateProvider(type = MarkertQuestionSQL.class, method = "updateNaireStatusSQL")
    void updateNaireStatus(@Param("id") String id, @Param("status") String status) throws Exception;

    /**
     * 修改问卷
     *
     * @param po
     * @throws Exception
     */
    @UpdateProvider(type = MarkertQuestionSQL.class, method = "updateNaireSQL")
    void updateNaire(MarketNairePoJo po) throws Exception;


    /**
     * 新增问卷
     *
     * @param po
     * @throws Exception
     */
    @UpdateProvider(type = MarkertQuestionSQL.class, method = "insertNaireSQL")
    void addNaire(MarketNairePoJo po) throws Exception;

    /***
     * 删除已存在问券明细记录
     * @param naaireNum
     */
    @DeleteProvider(type = MarkertQuestionSQL.class, method = "deleteNarireItemSQL")
    void deleteNaireItem(@Param("naireCode") String naaireNum);

    /***
     * 新增问券明细
     * @param nariePo
     */
    @InsertProvider(type = MarkertQuestionSQL.class, method = "insertNarireItemSQL")
    void addNaireInfo(MarketQuestionnairePojo nariePo);

    /***
     * 修改问券分值
     * @param id
     * @param score
     */
    @UpdateProvider(type = MarkertQuestionSQL.class, method = "updateNarireItemScoreSQL")
    void updateNaireInfoScore(@Param("id") String id, @Param("score") Integer score);

    /***
     * 查询调研问券明细树
     * @param pid
     * @param narieCode
     * @return
     */
    @SelectProvider(type = MarkertQuestionSQL.class, method = "queryNaireItemSQL")
    @Results({
            @Result(column = "id", property = "id", id = true),
            @Result(column = "label", property = "label"),
            @Result(column = "label_type", property = "labelType"),
            @Result(column = "value_type", property = "valueType"),
            @Result(column = "p_id", property = "pId"),
            @Result(column = "assgin_score", property = "assginScore"),
            @Result(column = "library_id", property = "libraryId"),
            @Result(column = "narie_code", property = "narieCode"),
            @Result(column = "company_id", property = "companyId"),
    })
    List<MarketQuestionnairePojo> queryNaireItem(@Param("pId") String pid, @Param("narieCode") String narieCode, @Param("companyId") String companyId);


    /***
     * 计算问题项分值
     */
    @UpdateProvider(type = MarkertQuestionSQL.class, method = "countItemScoreSQL")
    void countItemScore(@Param("companyId") String companyId, @Param("code") String code);

    /***
     * 计算问题分类分值
     */
    @UpdateProvider(type = MarkertQuestionSQL.class, method = "countItemGroupScoreSQL")
    void countItemGroupScore(@Param("companyId") String companyId, @Param("code") String code);

    /***
     * 查询问券总分值
     * @param pid
     * @param narieCode
     * @param companyId
     * @return
     */
    @SelectProvider(type = MarkertQuestionSQL.class, method = "queryTotalScoreSQL")
    @Results({
            @Result(column = "narie_code", property = "code"),
            @Result(column = "score", property = "score"),
            @Result(column = "company_id", property = "companyId"),
    })
    MarketNaireScorePoJo queryNarieTotalScore(@Param("code") String narieCode, @Param("companyId") String companyId);

}
